using System;
using System.Collections;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ServiceObjects;
using OracleInternal.TTC.Accessors;

namespace OracleInternal.TTC
{
	internal class TTCRowData : TTCMessage
	{
		internal const int COL_NULL_IND = -1;

		internal const int DATA_STORED_IN_DUPLICATE_STORE = -2;

		internal const int DUP_ROW_PADDING = -20;

		internal BitArray m_bitArrayOfColumns;

		internal bool m_bitVectorFound;

		internal int m_numberOfColumns;

		internal TTCRowData(MarshallingEngine mEngine)
			: base(mEngine, 7)
		{
		}

		internal void SetNumberOfColumns(int noOfColumns)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetNumberOfColumns);
			}
			try
			{
				m_numberOfColumns = noOfColumns;
				m_bitVectorFound = false;
				if (m_bitArrayOfColumns == null || m_bitArrayOfColumns.Count != m_numberOfColumns)
				{
					m_bitArrayOfColumns = new BitArray(m_numberOfColumns);
				}
				else
				{
					m_bitArrayOfColumns.SetAll(value: false);
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetNumberOfColumns, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetNumberOfColumns);
				}
			}
		}

		internal void SetBitVector(byte[] bitVec)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetBitVector);
			}
			try
			{
				if (m_bitArrayOfColumns != null)
				{
					m_bitArrayOfColumns.SetAll(value: false);
				}
				if (bitVec == null || bitVec.Length == 0)
				{
					m_bitVectorFound = false;
					return;
				}
				for (int i = 0; i < bitVec.Length; i++)
				{
					byte b = bitVec[i];
					for (int j = 0; j < 8; j++)
					{
						if ((b & (1 << j)) != 0)
						{
							m_bitArrayOfColumns.Set(i * 8 + j, value: true);
						}
					}
				}
				m_bitVectorFound = true;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetBitVector, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCRowData, OracleTraceFuncName.SetBitVector);
				}
			}
		}

		internal bool ReadRow(Accessor[] accessors, int length)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRow);
			}
			bool result = false;
			try
			{
				for (int i = 0; i < length && i < accessors.Length; i++)
				{
					if (accessors[i] == null)
					{
						continue;
					}
					if (m_bitVectorFound)
					{
						if (m_bitArrayOfColumns.Get(i))
						{
							accessors[i].UnmarshalOneRow();
							continue;
						}
						result = true;
						accessors[i].CopyRow_ScanOnly();
					}
					else
					{
						accessors[i].UnmarshalOneRow();
					}
				}
				m_bitVectorFound = false;
				return result;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRow, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRow);
				}
			}
		}

		internal void ReadRowNew(DataUnmarshaller dataUnMarshaller, Accessor[] accessors, int rowNumber)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRowNew);
			}
			try
			{
				for (int i = 0; i < accessors.Length; i++)
				{
					if (accessors[i] == null)
					{
						continue;
					}
					m_marshallingEngine.m_oraBufRdr.m_colDataStartOffsetIndexToUpdate = rowNumber * accessors.Length + i;
					if (m_bitVectorFound)
					{
						if (m_bitArrayOfColumns.Get(i))
						{
							accessors[i].UnmarshalColumnData();
							continue;
						}
						int colDataStartOffsetIndexToUpdate = m_marshallingEngine.m_oraBufRdr.m_colDataStartOffsetIndexToUpdate;
						if (rowNumber == 0)
						{
							if (dataUnMarshaller.DuplicateDataExistsForLastRow(i))
							{
								m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = -2;
							}
							else
							{
								m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = -1;
							}
							continue;
						}
						int num = colDataStartOffsetIndexToUpdate - accessors.Length;
						int num2 = m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[num];
						m_marshallingEngine.m_oraBufRdr.m_indexOfOASArray[colDataStartOffsetIndexToUpdate] = m_marshallingEngine.m_oraBufRdr.m_indexOfOASArray[num];
						if (-1 == num2)
						{
							m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = -1;
						}
						else if (-2 == num2)
						{
							m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = -2;
						}
						else if (num2 >= 0)
						{
							m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = -1 * (rowNumber - 1 - -20);
							int num3 = m_marshallingEngine.m_oraBufRdr.m_indexOfOASArray[num];
							if (num3 < 0)
							{
								if (!dataUnMarshaller.DuplicateDataExistsForLastRow(i))
								{
									m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[m_marshallingEngine.m_oraBufRdr.m_colDataStartOffsetIndexToUpdate] = -1;
								}
							}
							else
							{
								for (int j = num3; j < m_marshallingEngine.m_oraBufRdr.m_oraArrSegWithColRowInfoIndex; j++)
								{
									m_marshallingEngine.m_oraBufRdr.m_oraArrSegWithColRowInfo[j].m_maxRowNum = rowNumber;
								}
							}
						}
						else if (num2 <= -20)
						{
							m_marshallingEngine.m_oraBufRdr.m_colDataStartOffset[colDataStartOffsetIndexToUpdate] = num2;
							int num4 = -1 * (num2 - -20);
							for (int k = m_marshallingEngine.m_oraBufRdr.m_indexOfOASArray[num4 * accessors.Length + i]; k < m_marshallingEngine.m_oraBufRdr.m_oraArrSegWithColRowInfoIndex; k++)
							{
								m_marshallingEngine.m_oraBufRdr.m_oraArrSegWithColRowInfo[k].m_maxRowNum = rowNumber;
							}
						}
						accessors[i].m_lastRowProcessed++;
					}
					else
					{
						accessors[i].UnmarshalColumnData();
					}
				}
				m_bitVectorFound = false;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRowNew, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadRowNew);
				}
			}
		}

		internal void ReadBVC(int nbOfColumnSent)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadBVC);
			}
			int num = 0;
			try
			{
				m_bitArrayOfColumns.SetAll(value: false);
				int num2 = m_numberOfColumns / 8 + ((m_numberOfColumns % 8 != 0) ? 1 : 0);
				for (int i = 0; i < num2; i++)
				{
					byte b = (byte)((uint)m_marshallingEngine.UnmarshalUB1() & 0xFFu);
					for (int j = 0; j < 8; j++)
					{
						if ((b & (1 << j)) != 0)
						{
							m_bitArrayOfColumns.Set(i * 8 + j, value: true);
							num++;
						}
					}
				}
				if (num != nbOfColumnSent)
				{
					throw new Exception("T4CTTIrxd.unmarshalBVC: bits missing in vector");
				}
				m_bitVectorFound = true;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadBVC, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCRowData, OracleTraceFuncName.ReadBVC);
				}
			}
		}

		internal void ReInitialize()
		{
			m_bitVectorFound = false;
			m_numberOfColumns = 0;
		}
	}
}
